package net.bxx2004.zephyr.media;

import net.bxx2004.zephyr.Zephyr;
import net.bxx2004.zephyr.utils.Asynchronous;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class MusicPlayer {
    private SourceDataLine sd;
    public void play(String url){
        new Asynchronous(){
            @Override
            public String run() {
                stop();
                File file = new File(Zephyr.CACHE_PATH + "/music/cache.wav");
                File mp3File = new File(Zephyr.CACHE_PATH + "/music/cache.mp3");
                try {
                    Files.copy(new URL(url).openStream(),mp3File.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    transMP3(mp3File.getAbsolutePath(),file.getAbsolutePath(),true);
                    play(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }
        };
    }
    private void play(File file){
        new Asynchronous(){
            @Override
            public String run() {
                try {
                    //2、定义一个AudioInputStream用于接收输入的音频数据
                    AudioInputStream am = AudioSystem.getAudioInputStream(file);;
                    //4、使用AudioFormat来获取AudioInputStream的格式
                    AudioFormat af = am.getFormat();
                    //5、一个源数据行
                    sd = AudioSystem.getSourceDataLine(af);
                    sd.open();
                    sd.start();
                    int sumByteRead = 0; //读取的总字节数
                    byte [] b = new byte[320];//设置字节数组大小
                    //11、从音频流读取指定的最大数量的数据字节，并将其放入给定的字节数组中。
                    while (sumByteRead != -1) {//-1代表没有 不等于-1时就无限读取
                        sumByteRead = am.read(b, 0, b.length);//12、读取哪个数组
                        if(sumByteRead >= 0 ) {//13、读取了之后将数据写入混频器,开始播放
                            if (sd == null){
                                break;
                            }
                            sd.write(b, 0, b.length);
                        }
                    }
                    sd.drain();
                    sd.close();
                    clear();
                }catch (Exception e){}
                return null;
            }
        };
    }
    public void stop(){
        new Asynchronous() {
            @Override
            public String run() {
                sd = null;
                clear();
                return null;
            }
        };
    }
    private void clear(){
        for (File file : new File(Zephyr.CACHE_PATH + "/music").listFiles()){
            file.delete();
        }
    }
    public void transMP3(String path,String path1,boolean delete){
        File source = new File(path);
        File target = new File(path1);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("pcm_s16le");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setInputFormat("mp3");
        attrs.setOutputFormat("wav");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
        if (delete){
            source.delete();
        }
    }
}
